<template>
  <div class="tab-content group-detail">
    <BasicCard style="height: 100%; box-sizing: border-box">
      <BasicVxeTable ref="gridRef" v-bind="gridOption">
        <template #tools_left>
          <BasicVxeForm
            ref="formRef"
            :formOptions="formOptions"
            :searchLoading="gridOption.loading"
            @search="handleInit"
          >
          </BasicVxeForm>
        </template>
      </BasicVxeTable>
    </BasicCard>
  </div>
</template>

<script setup lang="ts">
  import { getwuLiaoAndJiTai } from '@/api/shengChanJianShi'
  import {
    getLuoTongCaiJiShuJuList,
    getLuoTongDanDingXiangQin,
    postLuoTongFenZhuXiangQing
  } from '@/api/shengChanJianShi'

  const { t } = useI18n()
  const url = window.location.href
  const gridOption = reactive<BasicGridProps>({
    extraConfig: {
      title: t('custom.详细报表'),
      isCustom: false,
      isExport: false,
      isFilterAndSort: true
    }, // 拓展的配置
    id: `group-detail-table`, // id用于缓存,务必配置
    toolbarConfig: {}, // 工具栏
    // exportConfig: {
    //   filename: '数据表'
    // }
    loading: true,
    height: '100%'
    // columns: GroupDetailTableColumns
  })

  const formRef = ref<BasicFormInstance<any>>()
  const formOptions = reactive<BasicFormProps>({
    data: {},
    items: [
      {
        field: 'equipment',
        title: t('custom.机台'),
        // span: 6,
        visible: true,
        itemRender: {
          name: 'SelectModal',
          props: {
            modalConfig: {
              title: t('system.列表选择', { name: t('custom.机台') })
            }
          }
        }
      },
      {
        field: 'form-actions',
        itemRender: {
          name: 'ActionButtons',
          children: [{ name: 'search' }]
        }
      }
    ]
  })

  const handleInit = () => {
    Promise.all([getMachineInfo(), getWindingList()]).then(() => {
      getGroupDetailList()
    })
  }

  const detailList = ref<any>([])
  const getGroupDetailList = () => {
    postLuoTongFenZhuXiangQing(machineArrayCode.value).then((res: any) => {
      res.forEach((val) => {
        val.machineRealInfo.groups.forEach((item) => {
          item.machineSpindleNo = val.machineName + '#' + item.P41002
          detailList.value.push(item)
        })
      })
      gridOption.data = detailList.value
      gridOption.loading = false
    })
  }

  const paramList = ref<any>([])
  const getWindingList = () => {
    return new Promise((resolve) => {
      getLuoTongCaiJiShuJuList(172, 2).then((res: any) => {
        resolve(res)
        let arr: any = []
        res.forEach((val) => {
          if (val.isShowWeb === true && val.code != 'P41002') {
            arr.push({
              title: val.name,
              field: val.code,
              minWidth: 70
            })

            paramList.value.push({
              collectMetaCode: val.code,
              collectMetaName: val.name
            })
          }
        })

        gridOption.columns = [
          {
            title: '机台号#组号',
            field: 'machineSpindleNo',
            minWidth: 110,
            fixed: 'left'
          },
          ...arr
        ]
      })
    })
  }

  const machineArrayCode = ref<any>([])
  const zongJiTaiList = ref<any>([])
  const getMachineInfo = () => {
    return new Promise((resolve) => {
      getwuLiaoAndJiTai('络筒').then((res: any) => {
        const backJson = [...res]
        if (backJson.length > 0) {
          zongJiTaiList.value = []
          for (let item of backJson) {
            zongJiTaiList.value.push(...item.childNodes)
          }
          zongJiTaiList.value = zongJiTaiList.value.sort((a, b) => {
            return a.text - b.text
          })
          // if (url.indexOf('msszh') !== -1) {
          // let arr = zongJiTaiList.value.filter((value) => {
          //   return value.text == '11'
          // })
          // this.quShiJiTaiArr = arr.map((value) => {
          //   return value.id
          // })
          // }
          // this.quShiJiTaiArr = [
          //   zongJiTaiList.value[0].id,
          //   zongJiTaiList.value[1].id,
          //   zongJiTaiList.value[2].id
          // ]
          const idArr: any = []
          const nameArr: any = []

          backJson.forEach((item, index) => {
            // 处理item的默认值
            item.text = item.text || '未设置品种'
            item.id = item.id || `10000${index}`

            // 过滤并提取选中的子节点
            const selectedChildren: any = item.childNodes.filter((child) => child.check)

            // 如果有选中的子节点，分别收集id和text
            if (selectedChildren.length > 0) {
              idArr.push(...selectedChildren.map((child) => child.id))
              nameArr.push(...selectedChildren.map((child) => child.text))
            }
          })
          if (url.indexOf('msszh') !== -1) {
            backJson.forEach((value) => {
              let itemArr = value.childNodes.filter((value1) => {
                return value1.text == '11'
              })
              if (itemArr.length !== 0) {
                machineArrayCode.value = [itemArr[0].id]
              }
            })
            // this.butonTitle = '11'
            resolve(res)
            return
          }
          // this.gongXuList = backJson
          machineArrayCode.value = idArr
          // if (nameArr.length == 2) {
          //   this.butonTitle = nameArr.join()
          // } else {
          //   this.butonTitle = nameArr
          //     .sort(function (a, b) {
          //       return a - b
          //     })
          //     .join('-')
          // }
          resolve(res)
        }
      })
    })
  }

  onMounted(() => {
    handleInit()
  })
</script>
<style scoped lang="scss"></style>
